package com.tapstitch.scm.common.utils;

import java.math.BigDecimal;
import java.text.DecimalFormat;

public class MoneyFormatUtil {

    public static final String ZERO = "0.00";

    public static long checkNull(Long price){
        return price == null ? 0 : price;
    }

    public static double checkNull(Double price){
        return price == null ? 0 : price;
    }

    public static long checkNull(Integer price){
        return price == null ? 0 : price;
    }

    public static String format(Double price) {
        if (price == null) {
            return "0.00";
        }
        DecimalFormat decimalFormat = new DecimalFormat("######0.00");
        return decimalFormat.format(price);
    }

    /**
     * BigDecimal 相减
     *
     * @param v1
     * @param v2
     * @return double
     */
    public static Double subtract(double v1, double v2) {
        BigDecimal n1 = new BigDecimal(Double.toString(v1));
        BigDecimal n2 = new BigDecimal(Double.toString(v2));
        return n1.subtract(n2).doubleValue();
    }

    /**
     * description 计算折扣价 x
     * @param price 原价
     * @param discountRate 折扣率 九折 = 0.9D
     * @return java.lang.Long
    **/
    public static Long doDisCount(Long price, Double discountRate){
        if (price == null || discountRate == null){
            return price;
        }
        BigDecimal priceDecimal = new BigDecimal(price);
        BigDecimal discountRateDecimal = new BigDecimal(discountRate);

        return priceDecimal.multiply(discountRateDecimal).longValue();
    }

    /**
     * 美分转成伊拉克币的元
     * @param usdFen：美分
     * @param orderRecordUsd2IqdRate：美元对伊拉克币汇率（100倍的值）
     * @return
     */
    public static String usdMoney2IqdMoney(Long usdFen, Long orderRecordUsd2IqdRate) {
    	 if (usdFen == null || orderRecordUsd2IqdRate == null || orderRecordUsd2IqdRate <= 0) {
             return null;
         }
    	 if(usdFen == 0) {
    		 return "0";
    	 }
         BigDecimal moneyYuan = new BigDecimal(usdFen).multiply(new BigDecimal(orderRecordUsd2IqdRate)).
                 divide(new BigDecimal(10000)).setScale(2, BigDecimal.ROUND_HALF_UP);
         DecimalFormat decimalFormat = new DecimalFormat("######0");
         return decimalFormat.format(moneyYuan);
    }

    /**
     * 将分转化为元，保留两位小数
     *
     * @param price 金额，整数 long 类型
     * @return String
     * @author wuqiong 2018-11-02 11:32
     */
    public static String convertTOMoney(Long price) {
        if (price == null) {
            return ZERO;
        }
        BigDecimal moneyYuan = new BigDecimal(price).
                divide(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP);
        DecimalFormat decimalFormat = new DecimalFormat("######0.00");
        return decimalFormat.format(moneyYuan);
    }

    /**
     * 将元转化为分
     *
     * @param price 金额，整数 long 类型
     * @return String
     * @author wuqiong 2018-11-02 11:32
     */
    public static Long convertTOMoney(String price) {
        if (StringUtils.isEmpty(price)) {
            return 0L;
        }
        BigDecimal moneyFen = new BigDecimal(price).
                multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP);
        return moneyFen.longValue();
    }

    // 将分转化为元，保留一位小数
    public static String convertTOMoneyOne(Long price) {
        if (price == null) {
            return "0.0";
        }
        BigDecimal moneyYuan = new BigDecimal(price).
                divide(new BigDecimal(100)).setScale(1, BigDecimal.ROUND_HALF_UP);
        DecimalFormat decimalFormat = new DecimalFormat("#######.#");
        return decimalFormat.format(moneyYuan);
    }

    /**
     * 将分转化为元，最多保留两位小数（如果结尾是 .00 则直接删除
     *
     * @param price 金额，整数 long 类型
     * @return String
     * @author wuqiong 2018-11-02 11:32
     */
    public static String convertTOMoneyWithoutDouble0(Long price) {
    	String convertTOMoney = convertTOMoney(price);
    	String dot00Suffix = ".00";
		if(convertTOMoney.endsWith(dot00Suffix)) {
    		return convertTOMoney.substring(0, convertTOMoney.length()-dot00Suffix.length());
    	}
    	return convertTOMoney;
    }


    public static Long convertYuanToFen(BigDecimal price) {
        if (price == null) {
            return 0L;
        }
        return price.multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP).longValue();
    }

    /**
     * 谨慎使用,抹掉角和分
     * */
    public static String convertTOMoney4Yuan(Long price) {
        if (price == null) {
            return "0";
        }
        BigDecimal moneyYuan = new BigDecimal(price).divide(new BigDecimal(100)).setScale(0, BigDecimal.ROUND_DOWN);
        DecimalFormat decimalFormat = new DecimalFormat("#");
        return decimalFormat.format(moneyYuan);
    }

    /**
     * 谨慎使用,向上取整
     * */
    public static String convertTOMoney4YuanUp(Long price) {
        if (price == null) {
            return "0";
        }
        BigDecimal moneyYuan = new BigDecimal(price).divide(new BigDecimal(100)).setScale(0, BigDecimal.ROUND_UP);
        DecimalFormat decimalFormat = new DecimalFormat("#");
        return decimalFormat.format(moneyYuan);
    }

	public static String convertTOMoneyTrimZero(Long price) {
		if (price == null) {
			return "0";
		}
		BigDecimal moneyYuan = new BigDecimal(price).divide(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP);
		DecimalFormat decimalFormat = new DecimalFormat("#######.##");
		return decimalFormat.format(moneyYuan);
	}

	/**
	 * description 分转换成元 返回浮点数
	 * @param price
	 * @return double
	**/
    public static double convertToDouble(Long price) {
        if (price == null) {
            return 0.0;
        }
        BigDecimal moneyYuan = new BigDecimal(price).
                divide(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP);
        return moneyYuan.doubleValue();
    }

    public static BigDecimal convertToBigDecimal(Long price) {
        if (price == null) {
            return new BigDecimal(0);
        }
        return new BigDecimal(price).
                divide(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP);
    }

    public static BigDecimal convertToBigDecimal(String price) {
        if (StringUtils.isEmpty(price)) {
            return new BigDecimal(0);
        }
        return new BigDecimal(price);
    }

    public static Long convertYuanToFen(String yuan) {
        if (StringUtils.isEmpty(yuan)) {
            return 0L;
        }
        return new BigDecimal(yuan).multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP).longValue();
    }

    public static Long convertYuanToFen(double yuan) {
        return new BigDecimal(yuan).multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP).longValue();
    }

    /**
     * 判断是否显示原价
     * @param showOriginalPrice
     * @param originalPrice
     * @param salePrice
     * @return
     */
    public static boolean isShowOriginalPrice(Boolean showOriginalPrice,Long originalPrice,Long salePrice){
        if(showOriginalPrice != null&&originalPrice != null&&salePrice != null
                &&showOriginalPrice&&originalPrice > salePrice){
            return true;
        }
        return false;
    }

	public static String formatMoney(Long money, String userLevelName) {
		String temp = "<font size=12 color='#fffffe' font-weight=bold>￥ </font><font size=26 color='#fffffe'>%s</font><font size=12 color='#fffffe'>.%s  </font><font size=14 color='#fffffe'>%s</font>";
		String priceStr = MoneyFormatUtil.convertTOMoney(money);
		String part1 = "";
		String part2 = "";
		if (priceStr.contains(".")) {
			part1 = priceStr.split("\\.")[0];
			part2 = priceStr.split("\\.")[1];
		} else {
			part1 = priceStr;
			part2 = "00";
		}
		return String.format(temp, part1, part2, userLevelName);
	}



	/**
	 * 人民币分 转换成对应区域的货币的 分
	 *
	 * @param rmbFen               人民币(单位:分)
	 * @param currencyExchangeRate 汇率
	 * @return 对应货币的分
	 */
	public static long convertRmbToTargetCurrency(Long rmbFen, Integer currencyExchangeRate) {
		//数据库是存的汇率都是相对10000人民币的汇率
		BigDecimal result = new BigDecimal(currencyExchangeRate).divide(new BigDecimal(10000),10, BigDecimal.ROUND_UP).multiply(new BigDecimal(rmbFen)).setScale(0, BigDecimal.ROUND_UP);
		return result.longValue();
//		double rate = currencyExchangeRate * 1.0 / 10000;
//		return (long) Math.ceil(rate * rmbFen);
	}

	/** Description: 将目标货币， 转换成 等汇率的人民币
	 *
	 * @param targetFen            目标货币 (单位:分)
	 * @param currencyExchangeRate 兑换人民币的 汇率
	 * @return long
	 * @author wuqiong 2019-01-24 11:32
	 */
	public static long convertTargetCurrencyToRmb(long targetFen, int currencyExchangeRate) {
		BigDecimal result =new BigDecimal(targetFen).divide(new BigDecimal(currencyExchangeRate).divide(new BigDecimal(10000), 10, BigDecimal.ROUND_UP), BigDecimal.ROUND_UP).setScale(0, BigDecimal.ROUND_UP);
		return result.longValue();
		//数据库是存的汇率都是相对10000人民币的汇率
//		double rate = currencyExchangeRate * 1.0 / 10000;
//		return (long) Math.ceil(targetFen / rate);
	}

    public static boolean isNumber(String str){
	    if(StringUtils.isEmpty(str)){
	        return false;
        }

        String reg = "^[0-9]+(.[0-9]+)?$";
        return str.matches(reg);
    }

    public static void main(String[] args) {
       /* DecimalFormat decimalFormat = new DecimalFormat("#######.##");
        System.out.println(decimalFormat.format(111));*/
//        System.out.println(convertTargetCurrencyToRmb(26809,5800));

        double v = convertToDouble(8000L);
        System.err.println(v);
    }
}

